package com.amigo.online.provider.ffmpeg.service.impl;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.amigo.online.provider.ffmpeg.common.ConstantValue;
import com.amigo.online.provider.ffmpeg.feignservice.VideoFeignService;
import com.amigo.online.provider.ffmpeg.rocketmq.consumer.VideoAddWatermarkConsumer;
import com.amigo.online.provider.ffmpeg.service.FfmpegService;
import com.amigo.online.provider.ffmpeg.util.FFMpegUtil;
import com.amigo.online.provider.ffmpeg.util.FastDFSClient;

@Component
public class FfmepgServiceImpl implements FfmpegService {

    private static final Logger logger = LoggerFactory.getLogger(FfmepgServiceImpl.class);

    @Autowired
    FFMpegUtil fFMpegUtil;

    @Autowired
    VideoFeignService videoFeignService;

    public void TestVideoHandle(String account) {
        String[] video720Array = {
                "http://storage.amigoonline.com.br/group1/M00/00/34/s38EElutXRCAfrsoAC7ybsgHjk05269954	720",
                "http://storage.amigoonline.com.br/group1/M00/00/19/s38EEluPdJ6AKsd9AH5RrQi6sqE6693602	750",
                "http://storage.amigoonline.com.br/group1/M00/00/11/s38EEluGSgKAdycHAJVgx5M_AMA5914461	774",
                "http://storage.amigoonline.com.br/group1/M00/00/11/s38EEluGSZ2AZgElAC8hhny3Tt81094257	776",
                "http://storage.amigoonline.com.br/group1/M00/00/13/s38EaluHRVSARH-GAB0KmI0q8PE1427704	778",
                "http://storage.amigoonline.com.br/group1/M00/00/2D/s38EalufhuKAcn0FALrY0gIGzrA1964187	788",
                "http://storage.amigoonline.com.br/group1/M00/00/04/s38Ealt16OOASdsfAE5OZbJqnTo2086701	796",
                "http://storage.amigoonline.com.br/group1/M00/00/33/s38EEluiXCWAV1sNAEhvf-wW_ls0191428	800",
                "http://storage.amigoonline.com.br/group1/M00/00/14/s38EEluHIMeAfIkVADMf4jl1KDA2707907	802",
                "http://storage.amigoonline.com.br/group1/M00/00/12/s38EEluHGEiAealfADzpXxBj7Yk5528052	804",
                "http://storage.amigoonline.com.br/group1/M00/00/2C/s38EalufhZ6AJCUSAFqxzbvVWZU9993486	844",
                "http://storage.amigoonline.com.br/group1/M00/00/04/s38Ealt16MuAKtkUACUHCHIv3XE2890452	850",
                "http://storage.amigoonline.com.br/group1/M00/00/13/s38EaluHRTyAbuupAK1yYGEolR09999098	940",
                "http://storage.amigoonline.com.br/group1/M00/00/10/s38EEluGRs6AX3gGAFWsLKTrxZA0969073	960",
                "http://storage.amigoonline.com.br/group1/M00/00/11/s38EEluGR7qAAtmVAI50yW0laVk5383653	1040",
                "http://storage.amigoonline.com.br/group1/M00/00/2F/s38EElugN8-AUNBhAG88fclTL7k4456365	1080",
                "http://storage.amigoonline.com.br/group1/M00/00/27/s38EElue-RqAHaEnAHKEclKhNJ40886915	1920"};

        String[] video540Array = {
                "http://storage.amigoonline.com.br/group1/M00/00/04/s38Ealt16NCAIhdsAE_c95L_CK85910710	540",
                "http://storage.amigoonline.com.br/group1/M00/00/11/s38EEluGTKCAdlo0AJ95548mAm43206301	544",
                "http://storage.amigoonline.com.br/group1/M00/00/2A/s38EElufTt2ASJQQAEPcFNWsiQA1071175	548",
                "http://storage.amigoonline.com.br/group1/M00/00/0B/s38Ealt7zwCARLTIAC6AGgKbhUo2471766	560",
                "http://storage.amigoonline.com.br/group1/M00/00/0B/s38EElt8rBeAN-xMAAs8cDI94KU6490336	568",
                "http://storage.amigoonline.com.br/group1/M00/00/06/s38Ealt16aiALQCrAHPq0fLLsis8227980	576",
                "http://storage.amigoonline.com.br/group1/M00/00/10/s38EEluGRUuAFb6gACMNuWFtb0g0181651	582",
                "http://storage.amigoonline.com.br/group1/M00/00/18/s38EaluN3EyAK36gAB0xP8Wt6Ok0265794	592",
                "http://storage.amigoonline.com.br/group1/M00/00/03/s38Ealt16KOAPZeWACokE13UgWk3889169	594",
                "http://storage.amigoonline.com.br/group1/M00/00/04/s38Ealt16MaAW-kMAM6d7XtFaMk5175365	598",
                "http://storage.amigoonline.com.br/group1/M00/00/24/s38EaluaKVOANgHpADmEhDYrpNg9523854	608",
                "http://storage.amigoonline.com.br/group1/M00/00/14/s38EaluHR1eACeHhADea8boxo-w6357641	622",
                "http://storage.amigoonline.com.br/group1/M00/00/25/s38EaluaMQSALnagACAGiIevzro3332843	634",
                "http://storage.amigoonline.com.br/group1/M00/00/10/s38EEluGRmyAEEgqAH5Ar4BMpR81118500	640"};
        String[] video360Array = {
                "http://storage.amigoonline.com.br/group1/M00/00/06/s38Ealt16VGAXaZvAG_fuGGzypU4085213	360",
                "http://storage.amigoonline.com.br/group1/M00/00/09/s38EElt2FKuAOPykABW7t91nJZQ7026671	368",
                "http://storage.amigoonline.com.br/group1/M00/00/14/s38EaluHRr2AVuDCABOh7pcsidc9943660	372",
                "http://storage.amigoonline.com.br/group1/M00/00/09/s38Ealt16pWAZz4yACDKNfZK9S01034729	374",
                "http://storage.amigoonline.com.br/group1/M00/00/13/s38EaluHRAOAFIKjABG1Th63FYQ3044952	388",
                "http://storage.amigoonline.com.br/group1/M00/00/0B/s38EElt7iomAe8c_AB0Xtc7QyLg8457261	398",
                "http://storage.amigoonline.com.br/group1/M00/00/2B/s38EElufVTeAXVUcAS5Ry9EO8X87036292	400",
                "http://storage.amigoonline.com.br/group1/M00/00/11/s38EEluGSXWAWO_OADhkZI08pvE6574913	410",
                "http://storage.amigoonline.com.br/group1/M00/00/11/s38EEluHGDaAKbg5AEijKaVgZFM4235846	414",
                "http://storage.amigoonline.com.br/group1/M00/00/12/s38EEluHGJSAVKM2AFRbI8cZZyw5971618	424",
                "http://storage.amigoonline.com.br/group1/M00/00/23/s38EaluaKA-AHrN9AA1H_nwq51c0061812	428",
                "http://storage.amigoonline.com.br/group1/M00/00/10/s38EEluGRCaAUjanABHSxNPQM2k4276998	436",
                "http://storage.amigoonline.com.br/group1/M00/00/10/s38EEluGRZiAD0L-ACKwZCI5xOo6350943	440",
                "http://storage.amigoonline.com.br/group1/M00/00/08/s38Ealt16h-AGEfUAB2lCUVcD7Y8469483	442",
                "http://storage.amigoonline.com.br/group1/M00/00/23/s38EaluaJrGAXVT2AA8QByWnbRU5145452	446",
                "http://storage.amigoonline.com.br/group1/M00/00/03/s38Ealt16J-AfBxqADYg-hZzzso6370800	448",
                "http://storage.amigoonline.com.br/group1/M00/00/04/s38Ealt16M6AaK66ABucnf3TYGw8542721	452",
                "http://storage.amigoonline.com.br/group1/M00/00/10/s38EEluGRkqAE-MJADyAG2Ic94Q7773662	466",
                "http://storage.amigoonline.com.br/group1/M00/00/03/s38Ealt16KKADiktACIxzjvKLso7709910	478",
                "http://storage.amigoonline.com.br/group1/M00/00/18/s38EaluN9bKAKdPCAB1-wCLWbaU6081829	480",
                "http://storage.amigoonline.com.br/group1/M00/00/24/s38EEluaBrKAYfZ6AA5juuiGs6E6868039	484",
                "http://storage.amigoonline.com.br/group1/M00/00/14/s38EaluHRXiAIMt-ADp3oaFLN7I0523415	492",
                "http://storage.amigoonline.com.br/group1/M00/00/05/s38Ealt16UOAbSpgAOZL82HHZ5s5156605	516",
                "http://storage.amigoonline.com.br/group1/M00/00/11/s38EEluGSQKAIBFOACVxq5V-h_82025974	518",
                "http://storage.amigoonline.com.br/group1/M00/00/2B/s38EElufU6iAHAy1AApDpR20u3s0537301	522",
                "http://storage.amigoonline.com.br/group1/M00/00/08/s38Ealt16mGAOs2XAHRTw-jPd244615998	530",
                "http://storage.amigoonline.com.br/group1/M00/00/29/s38EElufTQWADK9iADCIZ5kX5nQ1157033	532"};
        String[] video180Array = {
                "http://storage.amigoonline.com.br/group1/M00/00/22/s38EaluaJPWAczoxAAgTuaAXtN88121915	220",
                "http://storage.amigoonline.com.br/group1/M00/00/33/s38EElukneSAcOcVABVDlCh7-Rc2176977	224",
                "http://storage.amigoonline.com.br/group1/M00/00/08/s38Ealt16iuAIDFUADoFccJPq2Q4534405	226",
                "http://storage.amigoonline.com.br/group1/M00/00/18/s38EEluOFZKAN1ykABwxGujykJg8307648	232",
                "http://storage.amigoonline.com.br/group1/M00/00/2D/s38EalufhoWAT_jNAA-qevAYLJs3863599	258",
                "http://storage.amigoonline.com.br/group1/M00/00/17/s38EEluM0XaARcThABc9fJJ6BXE5848685	268",
                "http://storage.amigoonline.com.br/group1/M00/00/30/s38Ealug2d-AX_R4ABMlDe9vxgc7424722	272",
                "http://storage.amigoonline.com.br/group1/M00/00/24/s38EaluaKQiALgVQAAyt3JJdnWc8032210	292",
                "http://storage.amigoonline.com.br/group1/M00/00/2C/s38EalufhF-AcXEmACRFq0Cqjx86865668	298",
                "http://storage.amigoonline.com.br/group1/M00/00/2B/s38EElufWBaAfbd1ABH11JgeWQQ6853871	310",
                "http://storage.amigoonline.com.br/group1/M00/00/18/s38EEluOFF-AL0ngACJQRe__iYk0172503	320",
                "http://storage.amigoonline.com.br/group1/M00/00/12/s38EaluHQsSAa-IOABCU32SXz4I4790610	324",
                "http://storage.amigoonline.com.br/group1/M00/00/14/s38EaluHRX6AQRxFAA0pNY56EPA6389470	326",
                "http://storage.amigoonline.com.br/group1/M00/00/15/s38EaluHR9qAbMCiAAk2t9U3h6E8539102	328",
                "http://storage.amigoonline.com.br/group1/M00/00/07/s38Ealt16hKAR5g0ACA-qkvcxvE0855666	332",
                "http://storage.amigoonline.com.br/group1/M00/00/05/s38Ealt16RmABFGuABVFncHbtZE5718136	334",
                "http://storage.amigoonline.com.br/group1/M00/00/11/s38EEluGR46AMpeqAErDGSoFR6Y0659917	336"};

        for (String tempObj : video720Array) {
            String video = tempObj.substring(51, 85);
            String width = tempObj.substring(86, 89);
            handVideo(Integer.parseInt(width), video, account, "/home/amigo/online/video720/", "/home/amigo/online/video720target/");
        }

        for (String tempObj : video540Array) {
            String video = tempObj.substring(51, 85);
            String width = tempObj.substring(86, 89);
            handVideo(Integer.parseInt(width), video, account, "/home/amigo/online/video540/", "/home/amigo/online/video540target/");
        }

        for (String tempObj : video360Array) {
            String video = tempObj.substring(51, 85);
            String width = tempObj.substring(86, 89);
            handVideo(Integer.parseInt(width), video, account, "/home/amigo/online/video360/", "/home/amigo/online/video360target/");
        }

        for (String tempObj : video180Array) {
            String video = tempObj.substring(51, 85);
            String width = tempObj.substring(86, 89);
            handVideo(Integer.parseInt(width), video, account, "/home/amigo/online/video180/", "/home/amigo/online/video180target/");
        }
    }

    public void handVideo(int width, String video, String videoUserAccount, String temp, String target) {
        String waterMarkImg = ConstantValue.LOGO_XXL;
        int logo = width / 180;
        //int logo = width / 90;

        int logo_x = (int) (10 + logo * 1.5);
        int logo_y = (int) (10 + logo * 1.5);
        int font_x = (int) (10 + logo * 1.5);
        int font_y = 10 * logo + 30;
        int fontSize = 11 + logo * 3;
        int scale = 110 + logo * 30;
	/*	int scale = 200;
		if (width >= 720) {
			logo_x = logo_y = font_x = 17;
			font_y = 72;
			scale = 360 ;
		} else if (width >= 540 && width < 720) {
			fontSize = 18;
			logo_x = logo_y = font_x = 14;
			font_y = 55; //55比较好
			scale = 360 ;
		} else if (360 <= width && width < 540) {
			fontSize = 16;
			logo_x = logo_y = font_x = 11;
			font_y = 46;
			scale = 360 ;
		} else if (180 <= width && width < 360) {
			fontSize = 14;
			logo_x = logo_y = font_x = 8;
			font_y = 33;
			scale = 120 ;
		} else {
			fontSize = 12;
			font_y = 20;
		}*/
        StringBuilder videoTempFileBuilder = new StringBuilder(target).append(video)
                .append(ConstantValue.MP4);
        List<String> commandList = new LinkedList<>();
        commandList.add(ConstantValue.FFMPEG_PATH);
        commandList.add("-y");
        commandList.add("-i");
        commandList.add(temp + video);
        commandList.add("-c:v");
        commandList.add("libx264");
        commandList.add("-c:a");
        commandList.add("copy");
        commandList.add("-vf");
        StringBuilder videoCommandBuilder = new StringBuilder(256);
        videoCommandBuilder.append("[in]drawtext=fontfile=").append(ConstantValue.FONT_TYPE).append(":text=\\'ID:")
                .append(videoUserAccount).append("\\':x=").append(font_x).append(":y=").append(font_y)
                .append(":fontsize=").append(fontSize).append(":fontcolor=white")
                .append("[text];").append("movie=").append(waterMarkImg)
                .append(",scale=").append(scale).append(":-1")//水印图片等比缩放
                .append("[watermark];[text][watermark]overlay=").append(logo_x).append(":").append(logo_y)
                .append("[out]");
        commandList.add(videoCommandBuilder.toString());
        commandList.add(videoTempFileBuilder.toString());
		/*for(String obj : commandList) {
			System.out.println(obj);
		}	*/
        System.out.println("------------------" + width + "-------------------------");
        fFMpegUtil.dealVideoOperation(commandList);
    }

    @Override
    public void videoAddWaterMarkAndGetMusic(Map<String, Object> map) {
        int width = (int) map.get("width");
        String videoUrl = (String) map.get("video");
        //String videoTitle = (String) map.get("title");
        String videoTitle = videoUrl.substring(16);
        String videoUserAccount = (String) map.get("account");
        Integer userId = (Integer) map.get("userId");
        Integer videoId = (Integer) map.get("id");
        Integer musicId = (Integer) map.get("musicId");
        String waterMarkImg = ConstantValue.LOGO_XXL;
        float logo = (float) width / 180;
        int logo_x = (int) (10 + logo * 2);
        int logo_y = (int) (10 + logo * 2);
        int font_x = (int) (10 + logo * 2);
        int font_y = (int) (10 * logo + 35);
        int fontSize = (int) (8 + logo * 4);
        int scale = (int) (80 + logo * 40);

        StringBuilder videoTempFileBuilder = new StringBuilder(ConstantValue.VIDEO_TMP_PATH).append(videoTitle).append(ConstantValue.MP4);
        List<String> commandList = new LinkedList<>();
        commandList.add(ConstantValue.FFMPEG_PATH);
        commandList.add("-y");
        commandList.add("-i");
        commandList.add(ConstantValue.SERVER_URL + videoUrl);
        commandList.add("-c:v");
        commandList.add("libx264");
        commandList.add("-c:a");
        commandList.add("copy");
        commandList.add("-vf");
        StringBuilder videoCommandBuilder = new StringBuilder(256);
        videoCommandBuilder.append("[in]drawtext=fontfile=").append(ConstantValue.FONT_TYPE).append(":text=\\'ID:")
                .append(videoUserAccount).append("\\':x=").append(font_x).append(":y=").append(font_y)
                .append(":fontsize=").append(fontSize).append(":fontcolor=white")
                //.append(":shadowy=1:shadowcolor=").append(ConstantValue.FONT_COLOR)
                .append("[text];").append("movie=").append(waterMarkImg)
                .append(",scale=").append(scale).append(":-1")//水印图片等比缩放
                .append("[watermark];[text][watermark]overlay=").append(logo_x).append(":").append(logo_y)
                .append("[out]");
        commandList.add(videoCommandBuilder.toString());
        commandList.add(videoTempFileBuilder.toString());
        StringBuilder musicTempFileBuilder = new StringBuilder(100);
        if (musicId == null) {
            musicTempFileBuilder.append(ConstantValue.MUSIC_TMP_PATH).append(videoTitle).append(ConstantValue.MP3);
            commandList.add("-y");
            commandList.add("-f");
            commandList.add("mp3");
            commandList.add("-vn");
            commandList.add(musicTempFileBuilder.toString());
        }
        int duration = fFMpegUtil.dealVideoOperation(commandList);
        logger.info("------------------" + videoId + "-------------------------");
        File videoFile = new File(videoTempFileBuilder.toString());
        if (videoFile.exists()) {
            logger.info("------------------" + videoFile.length() + "时长为:" + duration + "-------------------------");
            String waterMarkUrl = FastDFSClient.uploadFile(videoFile);
            if (waterMarkUrl != null) {
                logger.info("------------------" + waterMarkUrl + "-------------------------");
                fFMpegUtil.removeTempFile(videoTempFileBuilder.toString());
                String musicUrl = "";
                if (musicId == null) {
                    musicUrl = FastDFSClient.uploadFile(new File(musicTempFileBuilder.toString()));
                    fFMpegUtil.removeTempFile(musicTempFileBuilder.toString());
                }
                logger.info("---------" + videoId + "---" + waterMarkUrl + "---" + musicUrl + "---" + userId);
                videoFeignService.updateVideoWaterMarkUrlV1(videoId, waterMarkUrl, musicUrl, userId, duration);
            }
        } else {
            logger.error("视频文件异常:" + videoTempFileBuilder.toString());
        }
    }
}
